package com.smedi.ismedi.authoritymanagement.core.application.query.impl;

import com.smedi.ismedi.authority.starter.response.AuthorityAccessResponse;
import com.smedi.ismedi.authority.starter.util.JsonHelper;
import com.smedi.ismedi.authority.starter.util.RedisStringHelper;
import com.smedi.ismedi.authoritymanagement.core.application.query.CurrentUserQueryService;
import com.smedi.ismedi.authoritymanagement.core.application.query.vo.CurrentUserInfoVo;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.AuthorityAccess;
import com.smedi.ismedi.cache.starter.util.RedisUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.smedi.ismedi.authority.starter.common.Constant.COLON;
import static com.smedi.ismedi.authority.starter.constant.AuthorityConstant.ISMEDI_ACCESS_TOKEN;
import static com.smedi.ismedi.authority.starter.context.CurrentUserHelper.infoContext;
import static com.smedi.ismedi.authority.starter.context.CurrentUserHelper.tokenContext;


/**
 * @Author: xulinjun
 * @Date: 2021-9-8 17:17
 */
@Slf4j
@Service
@AllArgsConstructor
public class CurrentUserQueryServiceImpl implements CurrentUserQueryService {
    private final JsonHelper jsonHelper;
    private final RedisUtil redisUtil;

    @Override
    public CurrentUserInfoVo getCurrentUserInfo() {
        String info = infoContext.get();
        if (null == info) {
            String token = tokenContext.get();
            return jsonHelper.jsonToObject(String.valueOf(redisUtil.get(ISMEDI_ACCESS_TOKEN + COLON + token)), CurrentUserInfoVo.class);
        }
        return jsonHelper.jsonToObject(info, CurrentUserInfoVo.class);
    }
}
